/**
 * lwlsr.com
 * Copyright (c) 2019-2019 All Rights Reserved.
 */
package com.example.util.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @author Jian
 * @version $Id TimeUtil.java, v 0.1 2019-07-16 17:04 Jian Exp $
 */
public class TimeUtil {

    private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 获取当天 00:00:00
     *
     * @return
     */
    public static String getDay() {
        Date date = new Date();
        return yyyyMMdd.format(date) + " 00:00:00";
    }


    public static String getNow() {
        return yyyyMMddHHmmss.format(new Date());
    }

    public static String get7Day() {
        return getDaysAgo(7) + " 00:00:00";
    }


    public static String getDaysAgo(int i) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -i);
        Date time = c.getTime();
        return yyyyMMdd.format(time);
    }


    public static String getNowEnd() {
        return getDaysAgo(0) + " 23:59:59";
    }


    public static String formatyyyyMMddHHmmss(Date date) {
        if (Objects.nonNull(date)) {
            try {
                return yyyyMMddHHmmss.format(date);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    public static String getNowHour(int i) {
        return getDaysAgo(0) + " " + (i < 10 ? "0" + i : i) + ":00:00";
    }


    public static String getNowHourAndMin(int i, int j) {
        return getDaysAndHourAndMin(0, i, j);
    }


    public static String getDaysAndHourAndMin(int days, int hour, int min) {
        return getDaysAgo(days) + " " + (hour < 10 ? "0" + hour : hour) + ":" + (min < 10 ? "0" + min : min) + ":00";
    }

    public static String getHms(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + "分:" + unitFormat(second) + "秒";
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + "时:" + unitFormat(minute) + "分:" + unitFormat(second) + "秒";
            }
        }
        return timeStr;
    }

    private static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

}